﻿using AutoMapper;
using MediatR;
using Provenance.Domain.DeepProcess;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.DeepProcess;
using Provenance.Read.Api.Application.Command.DeepProcess;

namespace Provenance.Read.Api.Application.CommandHandler.DeepProcess
{
    public class BackDeepProcessInfoCommandHandler : IRequestHandler<BackDeepProcessInfoCommand, ApiResult<DeepProcessInfo>>
    {
        private readonly IDeepProcessInfoRepository _deepProcessInfoRepository;
        private readonly ILogger<BackDeepProcessInfoCommandHandler> _logger;
        private readonly IMapper _mapper;
        public BackDeepProcessInfoCommandHandler(IDeepProcessInfoRepository deepProcessInfoRepository, ILogger<BackDeepProcessInfoCommandHandler> logger, IMapper mapper)
        {
            _deepProcessInfoRepository = deepProcessInfoRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<DeepProcessInfo>> Handle(BackDeepProcessInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj = await _deepProcessInfoRepository.GetAsync(x => x.ProductInfoId == request.ProductInfoId);
                if (obj == null)
                {
                    return ApiResult<DeepProcessInfo>.Failure("DeepProcessInfo not found");
                }
                return ApiResult<DeepProcessInfo>.Success(obj);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error getting DeepProcessInfo" + ex.Message);
                return ApiResult<DeepProcessInfo>.Failure("Error getting DeepProcessInfo");
            }
        }
    }
}
